package com.yzjs.localhost.service.sysLogininfor.impl;

import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dictionaries.DealDictionary;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmFamilyMembers.HrmFamilyMembersEntity;
import com.yzjs.localhost.entity.hrmTitleChange.HrmTitleChangeEntity;
import com.yzjs.localhost.entity.sysLogininfor.SysLogininforEntity;
import com.yzjs.localhost.dao.sysLogininfor.SysLogininforMapper;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmTitleChange.HrmTitleChangeService;
import com.yzjs.localhost.service.sysLogininfor.SysLogininforService;
import com.yzjs.localhost.service.tDataPersistenceAfter.TDataPersistenceAfterService;
import com.yzjs.localhost.service.tDataPersistenceBeforce.TDataPersistenceBeforceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service("sysLogininforService")
public class SysLogininforServiceImpl extends BaseServiceImpl<SysLogininforEntity, SysLogininforMapper> implements SysLogininforService {

    @Autowired
    AnalysisService analysisService;

    @Autowired
    SysLogininforService sysLoginInfoService;

    @Override
    public ComparisonResultDTO SysLoginInfoCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity before = dto.getBeforce();
        String beforeInfo = before.getInfo();
        Map<String, Object> beforeParse = JsonUtil.json2map(beforeInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();

        ComparisonResultDTO resultDTO = null;
        if (before.getOperatorCode().equals("add")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "");

        } else if (before.getOperatorCode().equals("edit")) {
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "");

        } else if (before.getOperatorCode().equals("remove")) {
            beforeParse.put("delFlag", "2");
            Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
            // 比对记录主键ID
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "");
        }

        // 记录表信息
        SysLogininforEntity loginEntity = JsonUtil.json2pojo(after.getInfo(), SysLogininforEntity.class);
        loginEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        sysLoginInfoService.insert(loginEntity);

        return resultDTO;
    }
}
